//logs.js
var app = getApp()
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    coupon:0,
    Vouchers:0,
    hide: true,
    cardimg: "https://wxcoms.sjwke.com/attachment/images/userbj.png",
    nav: [
      { 'img': '/images/myself/zhanghuyue.png', 'name': '账户余额', 'bind':'tradeinfo' },
      { 'img': '/images/myself/chongzhi.png', 'name': '充值', 'bind':'cash' },
      { 'img': '/images/myself/youhuiquan.png', 'name': '优惠券', 'bind':'youhui2' },
      { 'img': '/images/myself/jifen.png', 'name': '积分商城','bind':'jfsc' },],
    luck: '/images/myself/luck.png',
    list: [
      { 'img': '/images/myself/dizhi.png', 'name': '地址管理', 'right': '/images/myself/right.png', 'bind': 'map' },
      { 'img': '/images/myself/jifenguize.png', 'name': '积分规则', 'right': '/images/myself/right.png', 'bind':'bzzx' },
      { 'img': '/images/myself/qingchu.png', 'name': '清理缓存', 'right': '/images/myself/right.png','bind':'clear' },
      { 'img': '/images/myself/kefu.png', 'name': '联系客服', 'right': '/images/myself/right.png','bind':'zxkf' },],
      phone:null,
      card:'',
      yzm:'',
  },
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  // 领取会员卡弹框
  scanning: function () {
    this.setData({
      hide: !this.data.hide
    })
  },
  scanningclose: function () {
    console.log("-----------------关闭按钮点击了----------------")
    this.setData({
      hide: true
    })
  },
  // 跳转 地址列表页面
  toaddresslist: function () {
    console.log("------------------------点击了地址跳转-----------------------------");
    wx.navigateTo({
      url: '../addresslist/addresslist',
    })
  },
  // ——————————跳转到充值页面————————————
  cash: function (e) {
    wx: wx.navigateTo({
      url: 'cash',
    })
  },
  // ——————————跳转到交易明细页面————————————
  tradeinfo: function (e) {
    wx: wx.navigateTo({
      url: 'walletmx',
    })
  },
  clear:function(){
      wx.showToast({
        title: '清除成功',
      })
  },
  formSubmit:function(e){
      console.log(e);
      var code = e.detail.value.code;
      var name = e.detail.value.name;
      var phone = e.detail.value.phone;
      // var sr = e.detail.value.sr;
      var sr = this.data.date;
      e.detail.value.uid = wx.getStorageSync('users').id
      if(!name){
        wx.showToast({
          title: '请填写姓名',
        })
        return false;
      }

      if (!phone) {
        wx.showToast({
          title: '请填写电话',
        })
        return false;
      }

      if (!sr) {
        wx.showToast({
          title: '请选择生日',
        })
        return false;
      }

      var that = this;
      //检测输入的验证码是否正确
      if(code != that.data.yzm){
        wx.showToast({
          title: '验证码不正确',
        })
        return false;
      }
      app.util.request({
        'url': 'entry/wxapp/UseUserCard',
        'cachetime': '0',
        data: e.detail.value,
        success: function (res) {
          console.log(res.data)
          if(res.data.code == 200){
            //领取成功
            wx.showToast({
              title: '领取成功',
            })
            that.setData({
              card:res.data.card,
              hide:true,
            })
          }else{
            wx.showToast({
              title: '领取失败',
            })
          }
        },
      })
  },
  inputchange:function(e){
    this.setData({
      phone:e.detail.value
    })
      console.log(e);
  },
  getcode:function(){
      
      var phone = this.data.phone;
      if(!phone){
        wx.showToast({
          title: '请填写手机号',
        })
        return false;
      }

      var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
      if (!myreg.test(phone)) {
        wx.showToast({
          title: '格式不正确',
        })
        return false;
      }
      console.log(this.data.phone);
      var phone = this.data.phone;
      var that = this;
      // 网址信息
      app.util.request({
        'url': 'entry/wxapp/GetCode',
        'cachetime': '0',
        data: { phone: phone },
        success: function (res) {
          console.log(res.data)
          if(res.data.code == 200){
            that.setData({
              yzm:res.data.yzm
            })
            wx.showToast({
              title: '请注意查收',
            })
          }else{
            wx.showToast({
              title: '发送失败',
            })
            return false;
          }
        },
      })      
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     var end = util.formatTime(new Date).substring(0, 10).replace(/\//g, "-");
    console.log(end.toString())
    this.setData({
      date: end
    })
    var ptxx = wx.getStorageSync('bqxx')
    if(ptxx.more=='1'){
      var nbcolor = wx.getStorageSync('bqxx').color
    }
    if (ptxx.more == '2'){
      var nbcolor = wx.getStorageSync('nbcolor')
    }
    wx.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: "#ffffff",
    })
    var that = this
    var bqxx = wx.getStorageSync('bqxx')
    console.log(bqxx)
    this.setData({
      bqxx: bqxx,
      color:nbcolor
    })
    var user_id = wx.getStorageSync('users').id
    function getNowFormatDate() {
      var date = new Date();
      var seperator1 = "-";
      var seperator2 = ":";
      var month = date.getMonth() + 1;
      var strDate = date.getDate();
      if (month >= 1 && month <= 9) {
        month = "0" + month;
      }
      if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
      }
      var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
        + " " + date.getHours() + seperator2 + date.getMinutes()
        + seperator2 + date.getSeconds();
      return currentdate;
    }
    var time = getNowFormatDate()
    var current_time = time.slice(0, 10)//当前时间
    // 优惠券接口
    app.util.request({
      'url': 'entry/wxapp/Coupons',
      'cachetime': '0',
      data: { user_id: user_id },
      success: function (res) {
        console.log(res)
        var coupons = res.data.ok
        var coupon = []
        console.log(coupons.length)
        if (coupons.length>0){
          for (var i = 0; i < coupons.length; i++) {
            console.log(coupons[i])
            coupons[i].conditions = Number(coupons[i].conditions)
            if (current_time <= coupons[i].end_time) {
              console.log('有可以用的优惠券')
              if (coupons[i].state == 2) {
                coupon.push(coupons[i].length)
                console.log(coupons[i])
                that.setData({
                  coupon: coupon.length
                })
              }
            }else{
              console.log('没有可以用的优惠券')
              that.setData({
                coupon: 0
              })
            }
          }
        }else{
          that.setData({
            coupon: 0
          })
        }
        
      }
    })
    app.util.request({
      'url': 'entry/wxapp/Voucher',
      'cachetime': '0',
      data: { user_id: user_id },
      success: function (res) {
        console.log(res)
        var Voucher = res.data.ok
        var Vouchers = []
        if (Voucher.length>0){
          for (var i = 0; i < Voucher.length; i++) {
            Voucher[i].conditions = Number(Voucher[i].conditions)
            console.log(Voucher[i])
            if (current_time <= Voucher[i].end_time) {
              
              if (Voucher[i].state == 2) {
                console.log('有可以用的代金券')
                Vouchers.push(Voucher[i])
                that.setData({
                  Vouchers: Vouchers.length
                })

              } else {
                console.log('没有可以用的代金券')
                that.setData({
                  Vouchers: 0
                })
              }
            }
          }
        }else{
          that.setData({
            Vouchers: 0
          })
        }
       
      }
    })
    //获取头像和名字
    // wx.login({
    //   success: function (res) {
    //     console.log(res.data)
    //     wx.getUserInfo({
    //       success: function (res) {
    //         console.log(res)
    //         var userInfo = res.userInfo
    //         var nickName = userInfo.nickName
    //         var avatarUrl = userInfo.avatarUrl
    //         var gender = userInfo.gender //性别 0：未知、1：男、2：女
    //         var province = userInfo.province
    //         var city = userInfo.city
    //         var country = userInfo.country
    //         console.log(userInfo)
    //         that.setData({
    //           avatarUrl: userInfo.avatarUrl,
    //           nickName: userInfo.nickName,
    //         })
    //       },
    //       fail: function () {
    //         console.log("111")
    //       }
    //     })

    //   }
    // }),

    
    // 网址信息
    app.util.request({
      'url': 'entry/wxapp/Url',
      'cachetime': '0',
      success: function (res) {
        // console.log(res.data)
        that.setData({
          url: res.data
        })
      },
    })
    // 网址信息
    app.util.request({
      'url': 'entry/wxapp/Url2',
      'cachetime': '0',
      success: function (res) {
       console.log(res.data)
       wx.setStorageSync('url2', res.data)
      },
    })
    // fxst
    app.util.request({
      'url': 'entry/wxapp/FxSet',
      'cachetime': '0',
      success: function (res) {
        console.log(res.data)
        that.setData({
          fxset:res.data
        })
      },
    })
    // qdst
    app.util.request({
      'url': 'entry/wxapp/Signset',
      'cachetime': '0',
      success: function (res) {
        console.log(res.data)
        that.setData({
          qdset: res.data[0]
        })
      },
    })
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var ptxx = wx.getStorageSync('bqxx')
    if (ptxx.more == '1') {
      var nbcolor = wx.getStorageSync('bqxx').color
    }
    if (ptxx.more == '2') {
      var nbcolor = wx.getStorageSync('nbcolor')
    }
    wx.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: "#ffffff",
    })
    var that = this
    var bqxx = wx.getStorageSync('bqxx')
    console.log(bqxx)
    this.setData({
      bqxx: bqxx,
      color: nbcolor
    })
    var user_id = wx.getStorageSync('users').id
    // 积分
    app.util.request({
      'url': 'entry/wxapp/UserInfo',
      'cachetime': '0',
      data: { user_id: user_id },
      success: function (res) {
        console.log(res)
        console.log('---------------------------')
        that.setData({
          integral: res.data.total_score,
          wallet: res.data.wallet,
          card: res.data.card_num
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this
    // pageNum = 1;
    that.onLoad()
    that.onShow();
    wx.stopPullDownRefresh();
  },
  
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //————————————————————地图————————————
  map: function (e) {
    var that = this
    var user_id = wx.getStorageSync('users').id
    // wx.chooseAddress({
    //   success: function (res) {
    //     console.log(res.userName)
    //     console.log(res.postalCode)
    //     console.log(res.provinceName)
    //     console.log(res.cityName)
    //     console.log(res.countyName)
    //     console.log(res.detailInfo)
    //     console.log(res.nationalCode)
    //     console.log(res.telNumber)
    //     var tel = res.telNumber
    //     var address = res.countyName + res.detailInfo
    //     var user_name = res.userName
    //     app.util.request({
    //       'url': 'entry/wxapp/UpdAdd',
    //       'cachetime': '0',
    //       data: { user_id: user_id, user_tel: tel, user_address: address, user_name: user_name },
    //       success: function (res) {
    //         console.log(res)
    //       }
    //     })
    //   }

    // })

    wx.navigateTo({
      url: '/zh_dianc/pages/addresslist/addresslist',
    })
  },
  seller: function (e) {
    wx.navigateTo({
      url: '../seller/login'
    })
  },
  zxkf:function(){
    wx.navigateTo({
      url: 'kfzx'
    })
  },
  bzzx: function () {
    wx.navigateTo({
      // url: 'bzzx'
      url: '/zh_dianc/pages/rules/rules?jf=' + this.data.integral
    })
  },
  wallet: function (e) {
    wx.navigateTo({
      url: 'wallet'
    })
  },
  youhui: function (e) {
    // wx.navigateTo({
    //   // url: '../coupons/shop_coupons'
    //   // 
    //   url:"/zh_dianc/pages/coupons/shop_coupons"
    // })
  },
  youhui2: function (e) {
    wx.navigateTo({
      // url: '../coupons/mine_coupons'
      url: "/zh_dianc/pages/coupons/shop_coupons"
    })
  },
  jfsc: function (e) {
    wx.navigateTo({
      url: '../integral/integral'
    })
    // wx.navigateTo({
    //   url: '../seller/seller'
    // })
  },
  integral:function(e){
    wx.navigateTo({
      url: 'integral'
    })
  },
  wyrz: function (e) {
    wx.navigateTo({
      url: 'wyrz/authen'
    })
  },
  fx: function (e) {
    wx.navigateTo({
      url: 'distribution/yaoqing'
    })
  },
  czzx: function (e) {
    wx.navigateTo({
      url: 'cash'
    })
  },
  // 跳转小程序
  tzxcx: function (e) {
    var appid = this.data.bqxx.tz_appid
    console.log(appid)
    wx.navigateToMiniProgram({
      appId: appid,
      success(res) {
        // 打开成功
        console.log(res)
      }
    })
  }
})